// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    Sex:"",
    call:"",
    home:"",
    id:0,
    goods:[{
      pic:"../../images/mall/1.jpg",
      goods_name:"四维AR书籍《了不起的诗人》",
      money:5800
    },{
      pic:"../../images/mall/2.jpg",
      goods_name:"四维AR周边礼盒",
      money:2700
    },{
      pic:"../../images/mall/3.jpg",
      goods_name:"诗文通关手账",
      money:3000
    },{
      pic:"../../images/mall/4.jpg",
      goods_name:"唐代文创|元稹白居易徽章",
      money:1400
    },{
      pic:"../../images/mall/5.jpg",
      goods_name:"吟诵书签",
      money:2786
    },{
      pic:"../../images/mall/6.jpg",
      goods_name:"吟诵创意胶带",
      money:3600
    },{
      pic:"../../images/mall/7.jpg",
      goods_name:"手绘地图A",
      money:2800
    },{
      pic:"../../images/mall/8.jpg",
      goods_name:"手绘地图B",
      money:2700
    }]
  },

  /**
   * 购买提交购买产品序号
   */
  formSubmit:function(event){
    console.log(this.data.id)
    console.log(this.data.goods[this.data.id].money)
    wx.cloud.callFunction({
      name:'pay',
      data: {
        orderid: '2020'+new Date().getTime(), //订单号要唯一,
        money: this.data.goods[this.data.id].money, //需要支付的金额
        notice:"鹿鸣呦呦文创产品"
      },
      success: res=>{
        console.log("支付参数获取成功",res)
        wx.requestPayment({
          nonceStr: res.result.nonceStr,
          package: res.result.package,
          paySign: res.result.paySign,
          timeStamp: res.result.timeStamp,
          signType:'MD5',
          success(res){
            console.log('支付成功',res)
          },
          fail(res){
            console.log('支付失败',res)
          },
          complete(res){
            console.log('支付完成',res)
          }
        })
      },
      fail:res=>{
        console.log("支付参数获取失败",res)
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'data',
      success: (result) => {
        console.log(result.data)
        this.setData({
          name:result.data.name,
          Sex:result.data.Sex,
          call:result.data.call,
          home:result.data.home          
        })
      },
      fail: (res) => {},
      complete: (res) => {},
    })

    // 获取当前的商品id
    console.log("当前商品id为：",options.id);
    this.setData({
      id:options.id
    })
    
  },
})